package com.pengcheng.taskapplication.mvp.view.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.util.SPUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import static com.pengcheng.taskapplication.contants.ConstantValue.SpType.SP_IS_FIRST_ENTER_APP;

//import com.squareup.okhttp.OkHttpClient;

public class LaucherActivity extends AppCompatActivity {
    private SharedPreferences preference;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_laucher);
        boolean isFirst=isFirstEnterApp();
        if(isFirst){
            startDialog();
        }else{
            handler.postDelayed(runnableToLogin,2000);
        }

        //showPermission();

    }


    /**
     * 是否是首次进入APP
     */
    public  boolean isFirstEnterApp() {
        return SPUtils.getBoolean(LaucherActivity.this, SP_IS_FIRST_ENTER_APP, true);
    }

    /**
     * 保存首次进入APP状态
     */
    public  void saveFirstEnterApp() {
        SPUtils.putBoolean(LaucherActivity.this, SP_IS_FIRST_ENTER_APP, false);
    }


    private Handler handler = new Handler();
    private Runnable runnableToLogin = new Runnable() {
        @Override
        public void run() {
            String uid = SPUtils.getString(LaucherActivity.this, ConstantValue.SpType.userid);
            if(uid==null||uid.length()==0){
                Intent intent = new Intent(LaucherActivity.this, LoginActivityNew.class);
                startActivity(intent);
            }else{
                Intent intent = new Intent(LaucherActivity.this, MainActivity.class);
                startActivity(intent);
            }
            LaucherActivity.this.finish();
        }
    };
    private void showPermission() {
        new RxPermissions(this)
                .request(
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.CAMERA,
                        Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
                )
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        //同意权限
                    } else {
                        //拒绝权限

                    }
                });
    }

    AlertDialog dialog;
    private void startDialog() {
        dialog = new AlertDialog.Builder(this).create();
        dialog.show();
        //对话框弹出后点击或按返回键不消失;
        dialog.setCancelable(false);

        final Window window = dialog.getWindow();
        if (window != null) {
            window.setContentView(R.layout.dialog_index_show);
            window.setGravity(Gravity.CENTER);
            window.setWindowAnimations(R.style.BottomAnimation);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //设置属性
            final WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            params.dimAmount = 0.5f;
            window.setAttributes(params);
            WebView textView = window.findViewById(R.id.tv_1);
            TextView tvCancel= window.findViewById(R.id.tv_cancel);
            TextView tvAgree= window.findViewById(R.id.tv_sign_tip);
            tvCancel.setOnClickListener(view ->startFinish());
            tvAgree.setOnClickListener(view -> enterApp());
            String str = "感谢您选择车智行APP!我们非常重视您的个人信息和隐私保护。" +
                    "为了更好地保障您的个人权益，在您使用我们的产品前，" +
                    "请务必审慎阅读《隐私政策》和《用户协议》内的所有条款，" +
                    "" +
                    "";
            textView.loadUrl("file:///android_asset/serviceAgreement2.html");

//            SpannableStringBuilder ssb = new SpannableStringBuilder();
//            ssb.append(str);
//            final int start = str.indexOf("《");//第一个出现的位置
//            ssb.setSpan(new ClickableSpan() {
//                @Override
//                public void onClick(View widget) {
////                    getSupportDelegate().start(WebViewDelegate.create("隐私政策",
////                            JConstants.URL_POLICY));
//                    Intent intent = new Intent(LaucherActivity.this, Html5Activity.class);
//                    intent.putExtra("url","file:///android_asset/serviceAgreement.html");
//                    startActivity(intent);
////                    dialog.cancel();
////                    SPUtils.putBoolean(LaucherActivity.this,SP_IS_FIRST_ENTER_APP, true);
//                }
//
//                @Override
//
//                public void updateDrawState(TextPaint ds) {
//                    super.updateDrawState(ds);
//                    ds.setColor(LaucherActivity.this.getResources().getColor(R.color.red));       //设置文件颜色
//                    // 去掉下划线
//                    ds.setUnderlineText(false);
//                }
//
//            }, start, start + 6, 0);
//
//            final int end = str.lastIndexOf("《");//最后一个出现的位置
//
//            ssb.setSpan(new ClickableSpan() {
//                @Override
//                public void onClick(View widget) {
////                    getSupportDelegate().start(WebViewDelegate.create("用户协议",
////                            JConstants.URL_AGREEMENT));
//                    Intent intent = new Intent(LaucherActivity.this, Html5Activity.class);
//                    intent.putExtra("url","file:///android_asset/serviceAgreement.html");
//                    startActivity(intent);
////                    dialog.cancel();
////                    SPUtils.putBoolean(LaucherActivity.this,SP_IS_FIRST_ENTER_APP, true);
//                }
//
//                @Override
//
//                public void updateDrawState(TextPaint ds) {
//                    super.updateDrawState(ds);
//                    ds.setColor(LaucherActivity.this.getResources().getColor(R.color.red));       //设置文件颜色
//                    // 去掉下划线
//
//                    ds.setUnderlineText(false);
//                }
//
//            }, end, end + 6, 0);
//
//            textView.setMovementMethod(LinkMovementMethod.getInstance());
//            textView.setText(ssb, TextView.BufferType.SPANNABLE);BufferType
        }
    }

    private void enterApp() {//同意并继续，进入APP
        dialog.cancel();
        saveFirstEnterApp();
//        String uid = SPUtils.getString(LaucherActivity.this, ConstantValue.SpType.userid);
//        if(uid==null||uid.length()==0){
//            Intent intent =new Intent(LaucherActivity.this,LoginActivityNew.class);
//            startActivity(intent);
//        }else{
            Intent intent =new Intent(LaucherActivity.this,MainActivity.class);
            startActivity(intent);
//        }
        this.finish();
    }

    private void startFinish() {//更改状态，finish APP
        SPUtils.putBoolean(this,SP_IS_FIRST_ENTER_APP, true);
        dialog.cancel();
        this.finish();

    }




}
